<?php             
class Air_freightorder extends MY_Controller{				
	function __construct()
	{
		parent::__construct();
	}
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
        
    function index($order_status = 'new'){
        $this->load->model('Language');
        $data = array();
        foreach($this->Language->get_all() as $row){
                $data['langList'][$row['language_code']] = $row['language_name'];
        }
        $data['order_status'] = $order_status;
    	$this->_setLayout('admin/air_freightorder/air_freightorder_list','admin_layout',$data);
    }
            
	function add(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);
			if(empty($data)){
				$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));	    			    		
			}
			else{
				$this->load->model('Air_freightorder_model');
				if(isset($data['id'])){
					$this->Air_freightorder_model->update($data,$data['id']);
					$data_return = $this->Air_freightorder_model->get_by_id($data['id']);
					$data_return['edit'] = '<a href="'.base_url().'admin/air_freightorder/edit/'.$data['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
					$jsonOutPut = json_encode(array('success'=>true,'message'=>'Saved successfully','data'=>$data_return));
				}	
				else{	
					$id = $this->Air_freightorder_model->insert($data);
					$data_return = $this->Air_freightorder_model->get_by_id($id);
					$data_return['edit'] = '<a href="'.base_url().'admin/air_freightorder/edit/'.$id.'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
					$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$data_return));
				}		 
			}
		}catch (Exception $e){
			$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));	
		}
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}	
	}
			
	function delete(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
			$this->load->model('Air_freightorder_model');
			$this->Air_freightorder_model->delete($data);
			$jsonOutPut =  json_encode(array('success'=>true));
		}
		catch (Exception $e){
			$jsonOutPut =  json_encode(array('success'=>false));
		}
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}	
	}
			
	function grid(){
		$this->load->model('Air_freightorder_model');
		$callback = $_REQUEST['callback'];
		$data =  array();
		foreach($this->Air_freightorder_model->get_list($_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'id',(isset($_GET['dir']))?$_GET['dir']:'DESC',"LOWER(order_status)='".strtolower($_GET['order_status'])."'") as $row){
                        $row['view'] = '<a class="thickbox" href="#" onClick="thickBoxPopup('.$row['id'].')">View</a>';
			$data[] = array_merge($row,array('edit'=>'<a href="'.base_url().'admin/air_freightorder/edit/'.$row['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>'));                      
		}
		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','start'=>$_GET['start'],'total'=>$this->Air_freightorder_model->get_total("LOWER(order_status)='".strtolower($_GET['order_status'])."'"),'data'=>$data));
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}
	}
			
	function edit($id=0)
	{
		$this->load->model('Air_freightorder_model');
		$this->load->model('Language');
		$this->load->library('helpfunc');
		$data = array('datacontent' => (empty($id))?array():$this->Air_freightorder_model->get_by_id($id),
					  'message' => null);
		if($this->input->server('REQUEST_METHOD') == 'POST'){
			$input_data = $_POST;
			if(!empty($id) && $this->Air_freightorder_model->update($input_data,$id)){
				$data['message'] = 'Update successfully';
				$data['datacontent'] = array_merge($data['datacontent'],$input_data);
			}elseif($id = $this->Air_freightorder_model->insert($input_data,$id)){
				$data['message'] = 'Update successfully';
				$data['datacontent'] = $this->Air_freightorder_model->get_by_id($id);
			}
			else
				$data['message'] = 'Update fail';

		}
		foreach($this->Language->get_all() as $row){
			$data['langList'][$row['language_code']] = $row['language_name'];
		}
		$this->_setLayout('admin/air_freightorder/air_freightorder_edit', 'admin_layout', $data);
	}
        function details_view(){
            $this->load->model('Air_freightorder_model');
            $this->Air_freightorder_model->new_to_processing($_GET['id']);
            $row = $this->Air_freightorder_model->get_by_id($_GET['id']);
	    $data['details_list'] =   array('Shipper company:' => $row['shipper_company'],
                                            'Shipper represent:' => $row['shipper_represent'],
                                            'Consignee:' => $row['consignee'],
                                            'MAWB:' => $row['mawb'],
                                            'HAWB:' => $row['hawb'],
                                            'Description:' => $row['description'],
                                            'Quantity:' => $row['quantity'],
                                            'Gross weight:'=>$row['gross_weight'],
                                            'Real weight:'=>$row['real_weight'],
                                            'Volume CBM:'=>$row['volume_cbm'],
                                            'Charge weight:'=>$row['charge_weight'],
                                            'Tracking site:'=>$row['tracking_site'],
                                            'From:'=>$row['from'],
                                            'To:'=>$row['to'],
                                            'Flight:'=>$row['flight'],
                                            'Status:'=>$row['status'],
                                            'Term:'=>$row['term'],
                                            'Place of delivery:'=>$row['place_of_delivery'],
                                            'Date:'=>$row['date'],
                                            'Closing time:'=>$row['closing_time'],
                                            'Airfreight:'=>$row['airfreight'],
                                            'Airport charges:'=>$row['airport_charges'],
                                            'Remark:'=>$row['remark'],
                                            'Accounting information:'=>$row['accounting_information'],
                                            'Handling information:'=>$row['handling_information'],
                                            'Currency:'=>$row['currency'],
                                            'Chg code:'=>$row['chg_code'],
                                            'WT/VAL (PPD):'=>$row['wt_val_ppd'],
                                            'WT/VAL (COLL):'=>$row['wt_val_coll'],
                                            'Other (PPD):'=>$row['other_ppd'],
                                            'Other (COLL):'=>$row['other_coll'],
                                            'Declared value for carriage'=>$row['declared_value_for_carriage'],
                                            'Declared value for customs'=>$row['declared_value_for_customs'],
                                            'Shipping marks'=>$row['shipping_marks'],
                                            'Date of flight'=>$row['date_of_flight'],
                                            'Comment for agent'=>$row['comment_for_agent'],
                                            'Payment status'=>$row['payment_status'],
                                            'Agent'=>$row['agent_id'],
                                            'Order status'=>$row['order_status'],
                                            'Order day'=>date('d/m/Y',strtotime($row['order_day']))
                );

    	   $this->load->view('admin/air_freightorder/details_list',$data);
        }
        function update_status(){
            $this->load->model('Air_freightorder_model');
            return json_encode(array(
                'success' => $this->Air_freightorder_model->update_status($_GET['id'],$_GET['order_status'])
            ));
        }
}
?>